package br.com.yurikimo.ykkit;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.RelativeLayout;

public abstract class YKButton extends Button implements YKBaseComponent
{
	private YKLayoutManager layoutManager;
	private int normalImageID;
	private int clickedImageID;
	private boolean isCheckButton = false;
	private boolean isChecked = false;
	
	public YKButton(String label)
	{
		super(YKDirector.getActivity());
		setText(label);
		init();
	}
	
	public YKButton(String label,float x, float y, float width, float height, Context context)
	{
		super(context);
		
		setText(label);
		
		init();
		
		setBounds(x, y, width, height);
	}
	
	private void init(){
		layoutManager = new YKLayoutManager(this);
		
		if (YKDirector.getDefaultFontName() != null){
			setTypeface(YKFontManager.loadFont(YKDirector.getDefaultFontName(), YKFontManager.FONT_TTF));
		}
	}
	
	public void addToLayout(RelativeLayout relativeLayout){
		layoutManager.addToLayout(relativeLayout);
	}
	
	public boolean isCheckButton(){
		return isCheckButton;
	}
	
	public void setAsCheckButton(boolean isCheckButton){
		this.isCheckButton = isCheckButton;
	}
	
	public RelativeLayout getLayoutView(){
		return layoutManager.getLayoutView(getContext());
	}
	
	public float getObjectWidth(){
		return layoutManager.getObjectWidth();
	}
	
	public float getObjectHeight(){
		return layoutManager.getObjectHeight();
	}
	
	public float getObjectX(){
		return layoutManager.getObjectX();
	}
	
	public float getObjectY(){
		return layoutManager.getObjectY();
	}
	
	public void removeFromSuperView(){
		layoutManager.removeFromSuperView();
	}
	
	public void setBounds(float x, float y, float width, float height){
		layoutManager.setBounds(x, y, width, height);
	}
	
	public void setPosition(float x, float y){
		layoutManager.setPosition(x, y);
	}
	
	public void refreshLayout(){
		layoutManager.refreshLayout();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (normalImageID == 0 || clickedImageID == 0){
			return super.onTouchEvent(event);
		}
		
		if (!isCheckButton){
			switch (event.getAction())
			{
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_OUTSIDE:
				case MotionEvent.ACTION_CANCEL:
					setBackgroundResource(normalImageID);					
					break;
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_MOVE:					
					setBackgroundResource(clickedImageID);
					onClick(this);
					break;
				default:
					break;
			}
		}
		else{
			switch (event.getAction())
			{
				case MotionEvent.ACTION_UP:
					getObjectParent().uncheckButtons(this);
					setBackgroundResource(clickedImageID);
					onClick(this);
					break;
			}
		}
		return super.onTouchEvent(event);
	}
	
	public abstract void onClick(YKButton button);
	
	public void chek(boolean check){
		isChecked = check;
		
		if (isChecked){
			setBackgroundResource(clickedImageID);
		}
		else{
			setBackgroundResource(normalImageID);
		}
	}
	
	public void setImage(int normaImageID, int clickedImageID)
	{
		this.normalImageID = normaImageID;
		this.clickedImageID = clickedImageID;
		
		Bitmap bitmap = YKTextureManager.loadImage(normaImageID);//BitmapFactory.decodeResource(getContext().getResources(), normaImageID);
		
		layoutManager.setObjectWidth(bitmap.getWidth());
		layoutManager.setObjectHeight(bitmap.getHeight());
		
		Drawable drawable = new BitmapDrawable(bitmap);
				
		setBackgroundDrawable(drawable);
		
		refreshLayout();
	}
	
	public void setObjectParent(YKContainer parent){
		layoutManager.setObjectParent(parent);
	}
	
	public YKContainer getObjectParent(){
		return layoutManager.getObjectParent();
	}
}
